import axios from 'axios'
axios.defaults.baseURL = 'http://localhost:8888/api/private/v1/'
// axios拦截器，添加token
axios.interceptors.request.use(function (config) {
  let userToken = localStorage.getItem('userToken')
  if (userToken) {
    config.headers.Authorization = userToken
  }
  return config
}, function (error) {
  return Promise.reject(error)
})
// 登录请求
export const login = obj => axios.post('login', obj)
// 获取用户数据列表
export const userList = obj => axios.get('users', { params: obj })
// 添加用户
export const addUser = obj => axios.post('users', obj)
// 删除用户
export const delUser = id => axios.delete(`users/${id}`)
// 修改用户状态
export const changeUserState = (id, type) => axios.put(`users/${id}/state/${type}`)
// 编辑用户信息
export const editUser = (id, obj) => axios.put(`users/${id}`, obj)
// 获取角色列表
export const getRolesList = () => axios.get('roles')
// 分配角色
export const assignRoles = (id, rid) => axios.put(`users/${id}/role`, { rid })
// 获取权限列表数据
export const getRightsList = type => axios.get(`rights/${type}`)
// 删除角色指定权限
export const delRoleTag = (roleId, rightId) => axios.delete(`roles/${roleId}/rights/${rightId}`)
// 添加角色
export const addRole = obj => axios.post('roles', obj)
// 编辑角色
export const editRole = (id, obj) => axios.put(`roles/${id}`, obj)
// 删除角色
export const delRole = id => axios.delete(`roles/${id}`)
// 角色授权
export const roleRights = (roleId, checkArr) => axios.post(`roles/${roleId}/rights`, { rids: checkArr })
// 获取商品数据
export const getCate = (type, pagesize, pagenum) => axios.get('categories', {
  params: {
    type: type,
    pagesize: pagesize,
    pagenum: pagenum
  }
})
// 添加分类
export const addCate = obj => axios.post('categories', obj)
// 根据id查询分类
export const getCateById = id => axios.get(`categories/${id}`)
// 编辑提交分类
export const editCate = (id, catname) => axios.put(`categories/${id}`, { cat_name: catname })
// 删除分类
export const deleteCate = id => axios.delete(`categories/${id}`)
